package com.geovis.emergency.spd.controller.command;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.pc.service.IPcOneHundredPercentService;
import com.geovis.emergency.spd.cache.DictCache;
import com.geovis.emergency.spd.entity.command.pojo.vo.CommandOneHundredPercentVO;
import com.geovis.emergency.spd.entity.pc.entity.PcOneHundredPercent;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcDisasterConstant;
import com.geovis.emergency.spd.entity.system.entity.SystemDictData;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/11/3
 */
@RestController
@RequestMapping("/command/one/hundred/percent")
@RequiredArgsConstructor
@Api(value = "指挥调度大屏-五个百分百接口", tags = "指挥调度大屏-五个百分百接口")
@Slf4j
@Validated
@ApiSort(2)
public class CommandOneHundredPercentController extends BaseController<IPcOneHundredPercentService> {
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="appId",dataType="String",required=true,value="授权id"),
            @ApiImplicitParam(paramType="header",name="timestamp",dataType="String",required=true,value="当前时间戳(秒)"),
            @ApiImplicitParam(paramType="header",name="sign",dataType="String",required=true,value="签名"),
            @ApiImplicitParam(paramType="path",name="mainEventId",dataType="String",required=true,value="演练主事件id")
    })
    @ApiOperation(value = "列表", notes = "列表，签名生成方法：md5(appId+appSecret+timestamp)")
    @GetMapping("/list/{mainEventId}")
    public Result<List<CommandOneHundredPercentVO>> list(@PathVariable("mainEventId") String mainEventId) {
        List<SystemDictData> dictDataList = DictCache.getDictData(PcDisasterConstant.ONE_HUNDRED_PERCENT);
        List<PcOneHundredPercent> list = baseService.list(
                Wrappers.lambdaQuery(PcOneHundredPercent.class)
                        .eq(PcOneHundredPercent::getEventId, mainEventId)
                        .orderByDesc(PcOneHundredPercent::getCreateTime)
                        .last("limit "+dictDataList.size())
        );
        if (CollUtil.isNotEmpty(list)){
            List<CommandOneHundredPercentVO> collect = list.stream().map(e -> {
                CommandOneHundredPercentVO commandOneHundredPercentVO = BeanUtil.toBean(e, CommandOneHundredPercentVO.class);
                if (StrUtil.isNotBlank(commandOneHundredPercentVO.getType())){
                    commandOneHundredPercentVO.setTypeName(DictCache.getDictValue(PcDisasterConstant.ONE_HUNDRED_PERCENT,e.getType()));
                }
                return commandOneHundredPercentVO;
            }).collect(Collectors.toList());
            return Result.ok(collect);
        }else {
            List<CommandOneHundredPercentVO> collect = dictDataList.stream().map(s -> {
                CommandOneHundredPercentVO commandOneHundredPercentVO = new CommandOneHundredPercentVO();
                commandOneHundredPercentVO.setType(s.getDataKey());
                commandOneHundredPercentVO.setTypeName(s.getDataValue());
                commandOneHundredPercentVO.setCompleteCount(0);
                commandOneHundredPercentVO.setTotalCount(0);
                return commandOneHundredPercentVO;
            }).collect(Collectors.toList());
            return Result.ok(collect);
        }
    }
}
